package com.lc.sell.util;

import org.springframework.util.ResourceUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.UUID;

public class UploadImageUtils {

    public static String uploadImage(MultipartFile file) throws FileNotFoundException {
        // 本地
        String path = "http://localhost:8081";
        // 服务器
        //String path = "http://101.43.97.236:8081";
        if (file.isEmpty()) {
            System.out.println("文件为空");
        }
        String fileName = file.getOriginalFilename();  // 文件名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));  // 后缀名
        // 本地
        String filePath = "D:\\cx\\lc_sell\\src\\main\\resources\\static\\upload\\"; // 上传后的路径
        // 服务器
        //String filePath = "/software/upload/"; // 上传后的路径
        fileName = UUID.randomUUID() + suffixName; // 新文件名
        File dest = new File(filePath + fileName);
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            file.transferTo(dest);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String filename =path + "/image/" + fileName;
        return filename;
    }
}
